package com.lt.bookkeeping.coustomView;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.IdRes;

import com.lt.bookkeeping.R;

/**
 * @author LiuTong
 */
public class FlowLayout extends ViewGroup {
    private int leftMargin = 0, rightMargin = 0, topMargin = 0, bottomMargin = 0;

    public interface OnTagCancelClickListen {
        /****
         * 取消点击
         * @param view
         */
        void onClick(View view);
    }

    public OnTagCancelClickListen onTagCancelClickListen;

    public void setOnTagCancelClickListen( OnTagCancelClickListen onTagCancelClickListen) {
        this.onTagCancelClickListen = onTagCancelClickListen;
    }


    public FlowLayout(Context context) {
        this(context, null);
    }

    public FlowLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setMargin(int l, int r, int t, int b) {
        leftMargin = l;
        rightMargin = r;
        topMargin = t;
        bottomMargin = b;
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        /***
         * 测量子View 和 父View
         */
        int counts = getChildCount();
        int maxWidth = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();
        int sumWidth = 0;
        int sumHeight = 0;
        int sumChildWidth = 0;
        int childHeight = 0;

        int childMarginLeft = 0,
                childMarginRight = 0,
                childMarginTop = 0,
                childMarginBottom = 0;
        for (int i = 0; i < counts; i++) {
            View childView = getChildAt(i);
            if (childView == null) {
                return;
            }

            measureChild(childView, widthMeasureSpec,heightMeasureSpec);

            if (maxWidth < sumChildWidth + childView.getMeasuredWidth()) {
                sumHeight += childHeight;
                childHeight = 0;

                /***
                 * 获取全局最大的宽度
                 */
                sumWidth = Math.max(sumWidth, sumChildWidth);
                sumChildWidth = 0;
            }

            childHeight = Math.max(childHeight,
                    childView.getMeasuredHeight() + topMargin + bottomMargin + childMarginLeft + childMarginRight);
            sumChildWidth += childView.getMeasuredWidth() + leftMargin + rightMargin + childMarginTop + childMarginBottom;

            if (onTagCancelClickListen != null) {
                View tagView = childView.findViewById(R.id.tag_cancel);
                if (tagView == null) {
                    throw new IllegalArgumentException("未查询到相关id的view");
                }

                tagView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onTagCancelClickListen.onClick(childView);
                    }
                });
            }
        }
        sumHeight += childHeight;
        setMeasuredDimension(measureWidth(widthMeasureSpec, sumWidth), measureHeight(heightMeasureSpec, sumHeight));
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int maxWidth = r - l;
        int childCounts = getChildCount();
        int x = getPaddingLeft() + leftMargin,
                y = getPaddingTop() + topMargin;

        int childHeight = 0;
        for (int i = 0; i < childCounts; i++) {
            View view = getChildAt(i);
            if (maxWidth < x + view.getMeasuredWidth()) {
                y += childHeight;
                x = getPaddingLeft() + leftMargin;
            }
            view.layout(x, y, x + view.getMeasuredWidth(), y + view.getMeasuredHeight());
            x += view.getMeasuredWidth() + rightMargin;
            childHeight = Math.max(childHeight, view.getMeasuredHeight() + topMargin + bottomMargin);
        }
    }

    /***
     * 整个view的长宽
     * @param widthMeasure
     * @param sumWidth
     * @return
     */
    public int measureWidth(int widthMeasure, int sumWidth) {

        int mode = MeasureSpec.getMode(widthMeasure);
        int size = MeasureSpec.getSize(widthMeasure);
        int result = 0;
        if (mode == MeasureSpec.EXACTLY) {
            result = size;
            return result;
        }
        result = sumWidth + getPaddingLeft() + getPaddingRight();
        if (mode == MeasureSpec.AT_MOST) {
            result = Math.min(result, sumWidth);
            return result;
        }
        return result;
    }

    public int measureHeight(int heightMeasure, int sunHeight) {
        int mode = MeasureSpec.getMode(heightMeasure);
        int size = MeasureSpec.getSize(heightMeasure);
        int result = 0;
        if (mode == MeasureSpec.EXACTLY) {
            result = size;
            return result;
        }
        result = sunHeight + getPaddingTop() + getPaddingBottom();

        if (mode == MeasureSpec.AT_MOST) {
            result = Math.min(size, result);
        }
        return result;

    }
}
